package tech.yunland.shop.domain.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.moyuer.bsf.domain.model.User;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 会员卡
 *
 * @author 文卡<wkwenka@gmail.com>  on 17-6-27.
 */
@Entity
@Table(name = "member_card")
public class MemberCard extends BaseEntity {

    private String name; //卡名

    private String spell; //拼写

    @Column(length = 30)
    private String code; //代码

    @OneToMany(mappedBy = "memberCard")
    private List<MemberCardPictures> pictures; //插图

    @OneToMany(mappedBy = "owner")
    private List<MemberCardEntry> entries; //分录

    private Integer state = Integer.valueOf(0); //状态 -1删除 0未上架 1上架

    @Size(min = 0, max = 100)
    private Integer rate; // 折扣率%

    private BigDecimal price = BigDecimal.ZERO; //卡品价格

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "create_time", nullable = false, insertable = false, updatable = false)
    private Date createTime = new Date();

    @JSONField(serialize = false)
    @ManyToOne
    @JoinColumn(name = "creator_id")
    private User creator; //创建人

    @Transient
    private String creatorId;

    @Transient
    private String creatorName;

    @Transient
    private String pictureIds;

    @Transient
    private String pictureNames;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSpell() {
        return spell;
    }

    public void setSpell(String spell) {
        this.spell = spell;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<MemberCardPictures> getPictures() {
        return pictures;
    }

    public void setPictures(List<MemberCardPictures> pictures) {
        this.pictures = pictures;
    }

    public List<MemberCardEntry> getEntries() {
        return entries;
    }

    public void setEntries(List<MemberCardEntry> entries) {
        this.entries = entries;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public Integer getRate() {
        return rate;
    }

    public void setRate(Integer rate) {
        this.rate = rate;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public String getCreatorId() {

        if (StringUtils.isNotBlank(this.creatorId)){
            return this.creatorId;
        }
        if (this.creator != null){
            return this.creator.getId();
        }

        return creatorId;
    }

    public void setCreatorId(String creatorId) {
        this.creatorId = creatorId;
    }

    public String getCreatorName() {

        if (StringUtils.isNotBlank(this.creatorName)){
            return this.creatorName;
        }
        if (this.creator != null){
            return this.creator.getName();
        }

        return creatorName;
    }

    public void setCreatorName(String creatorName) {
        this.creatorName = creatorName;
    }

    public String getPictureIds() {

        if (pictureIds != null) {
            return pictureIds;
        }

        if (pictures != null) {
            Set<String> idSet = new HashSet<String>();
            for (MemberCardPictures memberCardPictures : pictures) {
                idSet.add(memberCardPictures.getPicture().getId());
            }

            pictureIds = StringUtils.join(idSet, ", ");
        }

        return pictureIds;
    }

    public void setPictureIds(String pictureIds) {
        this.pictureIds = pictureIds;
    }

    public String getPictureNames() {

        if (pictureNames != null) {
            return pictureNames;
        }

        if (pictures != null) {
            Set<String> nameSet = new HashSet<String>();
            for (MemberCardPictures memberCardPictures : pictures) {
                nameSet.add(memberCardPictures.getPicture().getFileName());
            }

            pictureNames = StringUtils.join(nameSet, ", ");
        }

        return pictureNames;
    }

    public void setPictureNames(String pictureNames) {
        this.pictureNames = pictureNames;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }
}
